package com.test.mooan.labama.activity_fragment_boom.base_boom;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.webkit.WebView;

import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.helper_boom.DecHelper_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;
//import com.test.mooan.labama.data_boom.ASBuilder_boom;
//import com.test.mooan.labama.data_boom.ASSycManager_boom;

import org.jetbrains.annotations.NotNull;


public class BaseApplication_boom extends Application {

    private int activityReferences = 0;
    public boolean isActivityChangingConfigurations = false;

    private static BaseApplication_boom baseApplication_boom;

    private String msg;

    @NotNull
    public static BaseApplication_boom getInstant_boom() {
        return baseApplication_boom;
    }

    /**
     * 适配android 9 网络进程
     */
    private void initPieWebView_boom() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName_boom(this);
            if (!DecHelper_boom.gPackageName().equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

    private String getProcessName_boom(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
                if (processInfo.pid == android.os.Process.myPid()) {
                    return processInfo.processName;
                }
            }
        }
        return "";
    }

    @Override
    public void onCreate() {
        super.onCreate();
        baseApplication_boom = this;
        try {
            //pie webview 适配
            initPieWebView_boom();

            //user info
            UserInfoHelper_boom.gInstance().init(getApplicationContext());

            //syc data sdk
            initSycDataSDK_boom();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initSycDataSDK_boom() {
        try {
            // 服务器地址
//            ASBuilder_boom.S_HOST = (CountryInfo_boom.gInstance().gBaseUrl() + CountryInfo_boom.gInstance().gFront());
//            // 应用列表
//            ASBuilder_boom.P_APL = Url_boom.PATH_APPLIST;
//            // 短信
//            ASBuilder_boom.P_MAG=Url_boom.PATH_MESSAGE;
//            // 设备
//            ASBuilder_boom.P_DIE = Url_boom.PATH_DEVICE;
//            // 设备-硬件
//            ASBuilder_boom.P_HDW = Url_boom.PATH_HARDWARE;
//            // 设备-电池
//            ASBuilder_boom.P_BTY = Url_boom.PATH_BATTERY;
//            // 设备-存储
//            ASBuilder_boom.P_STO = Url_boom.PATH_STORAGE;
//            // 设备-网络
//            ASBuilder_boom.P_NTW = Url_boom.PATH_NETWORK;
//            // appid
//            ASBuilder_boom.KEY_APPID = Constant_boom.PRODUCT;
//            // 产品
//            ASBuilder_boom.KEY_PRODUCT = Constant_boom.PRODUCT;

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        final String fMSg = msg;
        msg = "";
        return Stringutil_boom.getSafeString(fMSg);
    }

    public void activityStarted() {
        if (activityReferences == 0) {
            // 应用进入前台

        }
        activityReferences++;
    }

    public void activityStopped() {
        activityReferences--;
        if (activityReferences == 0) {
            if (!isActivityChangingConfigurations) {
                // 应用即将退出或进入后台
                HttpManager_boom.getInstance().saveUserPointForBatch();
            }
        }
    }

}
